/**
 * 文件名：LoginController.java
 *
 * 创建人：虞云波 - .com
 *
 * 创建时间：2017年10月30日 下午2:54:04
 *
 * 版权所有：江苏晟邦网络科技有限公司
 */
package com.cbwl.eoas.web.controller.login;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cbwl.eoas.api.client.service.sysconfig.ISmsMessageApi;
import com.cbwl.eoas.cms.api.sysconfig.ISysUserService;
import com.cbwl.eoas.cms.model.eoas.AppSmsLogDTO;
import com.cbwl.eoas.cms.model.sysconfig.SysUserDTO;
import com.cbwl.eoas.common.cache.redis.RedisCache;
import com.cbwl.eoas.common.cache.redis.RedisKeyPrefix;
import com.cbwl.eoas.common.framework.session.SecurityUserDTO;
import com.cbwl.eoas.common.util.Md5Util;
import com.cbwl.eoas.common.util.UuidGenerator;
import com.cbwl.eoas.common.util.constain.DicDetailConstant;
import com.cbwl.eoas.common.util.jpush.JPush;
import com.cbwl.eoas.common.web.Result;
import com.cbwl.eoas.web.framework.annotation.InterceptorUrl;
import com.fasterxml.jackson.annotation.JsonFormat.Value;

/**
 * <p>[描述信息：说明类的基本功能]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月30日 下午2:54:04
 */


@RestController
@RequestMapping("/v1/login")
public class LoginController {

	
	@Autowired
	private ISysUserService sysUserService;
	
	
	@Autowired
	private RedisCache<String,String> redisCache;
	
	
	@Autowired
	private ISmsMessageApi ismsMessageApi;
	
	
	/**
	 * 
	 * <p>app端用户登录</p>
	 * 
	 * @param request
	 * @param bean
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年12月4日 下午5:45:32
	 */
	@RequestMapping(value="/checkLogin",method = RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object checkLogin(HttpServletRequest request,SysUserDTO bean){
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		if(StringUtils.isEmpty(bean.getUserLoginname()) || StringUtils.isEmpty(bean.getUserPassword())){
			result.setCode("CBWL-000001");
			return result;
		}
		
		// 通过登录名获取用户信息
		SysUserDTO sysuser = new SysUserDTO();
		sysuser.setUserLoginname(bean.getUserLoginname());
		SysUserDTO sysUser = sysUserService.getUserDTOByParam(sysuser);

		if (sysUser == null) {
			result.setCode("CBWL-000006");
			return result;
		}
		
		if(sysUser.getUserPassword().equals(Md5Util.encodePassword(bean.getUserPassword(), sysUser.getUserJobno()))){
			//用户登录成功 生成tokenid
			String tokenId = UuidGenerator.getUuid();
			sysUser.setTokenId(tokenId);
			
			//用户存活时间，单位小时
			int liveTime = 24*3;
			String time =redisCache.getStr(RedisKeyPrefix.PARAM_PREFIX+"key_tokenId_livetime");
			if(!StringUtils.isEmpty(time)){
				liveTime =Integer.valueOf(time);
			}
			redisCache.putStr(tokenId,String.valueOf(sysUser.getId()),3600*liveTime);
			
			//返回用户基本信息
			SecurityUserDTO sbean = sysUserService.getSecurityUserByLoginName(sysUser);
			sbean.setId(null);
			
			sbean.setTokenId(tokenId);
			sbean.setKey(Md5Util.encodePassword(sbean.getUserJobno(),JPush.SALT));
			result.setData(sbean);
			return result;
			
		}else{
			result.setCode("CBWL-000006");
			return result;
		}
		
	}
	
	
	/**
	 * 
	 * <p>根据工号获取对应的手机号码</p>
	 * 
	 * @param userJobno
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年11月1日 下午2:47:24
	 */
	@RequestMapping(value="/getTelephone",method=RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object getTelephone(HttpServletRequest request,String userJobno){
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		
		SysUserDTO bean = new SysUserDTO();
		bean.setUserJobno(userJobno);
		List<SysUserDTO> userList = sysUserService.queryObjectList(bean);
		
		if(userList != null && userList.size()>0){
			String phone = userList.get(0).getUserPhone();
			if(!StringUtils.isEmpty(phone)){
				Map<String,String> map = new HashMap<>();
				map.put("userPhone",phone);
				result.setData(map); 
			}else{
				result.setCode("CBWL-000012");
			}
		}else{
			result.setCode("CBWL-000014");
		}

		return result;
	}
	
	/**
	 * 
	 * <p>获取短信验证码</p>
	 * 
	 * @param telephone
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @throws Exception 
	 * @date: Created on 2017年11月1日 上午11:13:08
	 */
	@RequestMapping(value="/getCode",method = RequestMethod.POST)
	@InterceptorUrl(validsecond=0,validnum=1)
	public Object getCode(HttpServletRequest request,AppSmsLogDTO bean,SysUserDTO userBean) throws Exception{
		
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		if(StringUtils.isEmpty(userBean.getUserJobno()) || StringUtils.isEmpty(bean.getSmsTelephone())){
			result.setCode("CBWL-000010");
			return result;
		}
		
		bean.setSmsSrc(DicDetailConstant.sms_src_1);
		bean.setSelectType("2");
		
		//发送短信验证码
		return ismsMessageApi.saveAndSendMessage(bean,userBean);
	}
	
	
	
	/**
	 * 
	 * <p>校验验证码是否正确</p>
	 * 
	 * @param userPhone
	 * @param smsCode
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年11月1日 下午3:44:10
	 */
	@RequestMapping(value="/checkCode",method=RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object checkCode(HttpServletRequest request,AppSmsLogDTO bean){
		
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		if(StringUtils.isEmpty(bean.getSmsCode()) || StringUtils.isEmpty(bean.getSmsTelephone())){
			result.setCode("CBWL-000010");
			return result;
		}
		
		bean.setSmsSrc(DicDetailConstant.sms_src_1);
		bean.setSelectType("1");
		return ismsMessageApi.checkCode(bean);
	}
	
	
	
	
	
	/**
	 * 
	 * <p>密码重置</p>
	 * 
	 * @param userPhone
	 * @param userPassword
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @throws Exception 
	 * @date: Created on 2017年11月1日 下午3:44:57
	 */
	@RequestMapping(value="/passwordRest",method=RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object passwordRest(HttpServletRequest request,SysUserDTO bean) throws Exception{
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		if(StringUtils.isEmpty(bean.getUserJobno()) || StringUtils.isEmpty(bean.getUserPassword())){
			result.setCode("CBWL-000001");
			return result;
		}
		
		SysUserDTO sysuser = new SysUserDTO();
		sysuser.setUserLoginname(bean.getUserJobno());
		SysUserDTO sysUser = sysUserService.getUserDTOByParam(sysuser);
		
		bean.setModifier(sysUser.getId());
		bean.setGmtModified(new Date());
		bean.setId(sysUser.getId());
		//更新密码
		sysUserService.updateUserInfo(bean);
		
		Map<String, String> map = new HashMap<>();
		map.put("userJobno", bean.getUserJobno());
		map.put("userPassword", bean.getUserPassword());
		result.setData(map);
		return result;
	}
	
	
	/**
	 * 
	 * <p>用户注销</p>
	 * 
	 * @param bean
	 * @return
	 * @throws Exception
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年11月2日 上午10:43:00
	 */
	@RequestMapping(value="/loginOut",method=RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object loginOut(HttpServletRequest request,SysUserDTO bean) throws Exception{
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		if(StringUtils.isEmpty(bean.getTokenId())){
			result.setCode("CBWL-000010");
			return result;
		}
		//根据tokenID 清空redis缓存
		redisCache.removeStr(bean.getTokenId());
		return result;
	}
	
	
	
	
	
	
	
	

	
	
	
	
}
